<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;

class adminController extends Controller
{
    public function index()
    {
        //查询数据
        $result = DB::table('admin')->get();
        //加载页面 传送数据
        return view('admin/admin', ['result' => $result]);
    }

    public function add()
    {
        //查询角色数据
        $list = DB::table('role')->get();
        //加载页面 传送数据
        return view('admin/adminadd', ['list' => $list]);
    }

    //后台添加数据方法
    public function insert(Request $request)
    {
        //接收信息数据
        $a_name = $request->a_name;
        $a_pass = md5($request->a_pass);
        $a_email = $request->a_email;
        $a_type = $request->a_type;
        $a_tragtime = date('Y-m-d');
        $a_lasttime = date('Y-m-d');

        //批量插入
        $data = array(
            'a_name' => $a_name,
            'a_pass' => $a_pass,
            'a_email' => $a_email,
            'a_type' => $a_type,
            'a_tragtime' => $a_tragtime,
            'a_lasttime' => $a_lasttime,
        );

        //接收数据角色id和用户新增id
        $result = DB::table('admin')->insertGetId($data);

        //获取用户id和角色id 插入（userole）里
        $id = $request->or_name;
        foreach ($id as $v) {
            //把id插入表中（limt）
            $li_id = DB::insert("insert into `userole` (`ro_id`,`ro_rid`) VALUES ($result,$v)");
        }

        //判断数据
        if ($li_id) {
            //是trun跳转页面
            return redirect('/admin/user');
        } else {
            return redirect('/admin/add');
        }
    }

    //后台编辑用户
    public function upd($id)
    {
        //接收数据id
        $id = $id;

        //查询数据
        $result = DB::select("select `id`,`a_name`,`a_email`,`a_pass`,`a_type`,`a_tragtime`,`a_lasttime` from `admin` where id=?", [$id]);
        //查询角色的数据信息
        $li = DB::select("select `or_name` 
              from `role` r,`admin` u,`userole` l
              where r.`id` = l.`ro_rid` and u.`id` = l.`ro_id` and u.`id`=" . $id);

        //查询所有的角色数据
        $role = DB::table('role')->get();
        //加载页面 传送数据
        return view('admin/adminupdate', ['result' => $result, 'role' => $role, 'li' => $li]);

    }

    //编辑方法
    public function update(Request $request)
    {
        //进来之后无论是否改动其他数据先更新角色数据（无论是元数据还是新数据）
        //接传来的数据
        $or_name = $request->or_name;//角色id
        $uid = $request->id;//用户id
        //修改（userole）的状态
        //先删除原来的数据之后添加新的数据（没毛病）
        $user_del = DB::delete("delete from `userole` where `ro_id`=" . $uid);
        //插入新的数据
        foreach ($or_name as $v) {
            $add = DB::insert("insert into `userole` (`ro_id`,`ro_rid`) VALUES ($uid,$v)");
        }
        if ($add) {
            //接收信息数据
            $u_id = $request->id;
            $a_name = $request->a_name;
            $a_pass = md5($request->a_pass);
            $a_email = $request->a_email;
            $a_type = $request->a_type;
            $a_lasttime = date('Y-m-d');

            //处理数据
            $result = DB::table('admin')
                ->where('id', $u_id)
                ->update(
                    [
                        'a_name' => $a_name,
                        'a_pass' => $a_pass,
                        'a_email' => $a_email,
                        'a_type' => $a_type,
                        'a_lasttime' => $a_lasttime,
                    ]
                );
            //判断数据跳转
            if ($result) {
                //true跳转重定向
                return redirect('/admin/user');
            }
        } else {
            //true跳转重定向
            return redirect('/admin/upd/' . $uid);
        }

    }

    //接收ajax删除
    public function del($id)
    {
        //接收数据
        $id = $id;
        //删除之前把(userole)里的数据也删除
        $list = DB::delete('delete from `userole` where ro_id=' . $id);

        $result = DB::delete('delete from `admin` where id=' . $id);

        if ($result) {
            echo 1;
        } else {
            echo 2;
        }
    }

}
